#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 500
#define tolower(c) (c>='A'&&c<='Z' ? 'a'-'A'+c:c)
struct Node
{
    char na[51];
    char au[21];
    char cn[31];
    char dat[11];
};
typedef struct Node Book;
Book books[maxn];
int num = 0;
int cmp(const void *a, const void *b);
int Index(char s[], char t[]);
void input(Book books[], char *line);
char *myStrncpy(char *dest, const char *src, size_t n);
int main()
{
    FILE *IN, *OUT;
    if((IN = fopen("books.txt", "r")) == NULL)
    {
        printf("The file do not exist.\n");
        return -1;
    }
    OUT = fopen("ordered.txt", "w");
    int op;
    char line[1025];
    while(fgets(line, 1024, IN) != NULL)
        input(books, line);
    qsort(books, num, sizeof(Book), cmp);
    while(scanf("%d\n", &op) == 1)
    {
        if(op == 1)
        {
            fgets(line, 1024, stdin);
            input(books, line); //这个先不改成往后挪一位
            qsort(books, num, sizeof(Book), cmp);
        }
        else if(op == 2)
        {
            char s[51];
            gets(s);
            for(int i=0;i<num;i++)
            {
                if(Index(books[i].na, s) > 0)
                    printf("%-50s%-20s%-30s%-10s\n",books[i].na,books[i].au,books[i].cn,books[i].dat);
            }
        }
        else if(op == 3)
        {
            char s[51];
            gets(s);
            for(int i=num-1;i>=0;i--)
            {  
                if(Index(books[i].na, s) > 0)
                {
                    if(i < num-1)
                    {
                        for(int j=i;j<num-1;j++)
                            books[j] = books[j+1];
                    }
                    strcpy(books[num-1].na, "\0");
                    strcpy(books[num-1].au, "\0");
                    strcpy(books[num-1].cn, "\0");
                    strcpy(books[num-1].dat, "\0");
                    num--;
                }
            }
        }
        else
            break;
    }
    qsort(books, num, sizeof(Book), cmp);
    for(int i=0;i<num;i++)
        fprintf(OUT, "%-50s%-20s%-30s%-10s\n", books[i].na,books[i].au,books[i].cn,books[i].dat); 
    fclose(IN);
    fclose(OUT);
    return 0;
}

int cmp(const void *a, const void *b)
{
    return (strcmp(((Book *)a)->na, ((Book *)b)->na));
}

int Index(char s[], char t[])
{
    int i, j, k;
    for(i=0;s[i]!='\0';i++)
    {
        for(j=i,k=0;t[k]!='\0' && tolower(s[j])==tolower(t[k]);j++,k++);
        if(t[k] == '\0')
        return 1;
    }
    return -1;
}

char *myStrncpy(char *dest, const char *src, size_t n)
{
    int size = sizeof(char)*(n+1);
    char *tmp = (char*)malloc(size);  // 开辟大小为n+1的临时内存tmp
    if(tmp)
    {
        memset(tmp, '\0', size);  // 将内存初始化为0
        memcpy(tmp, src, size-1);  // 将src的前n个字节拷贝到tmp
        memcpy(dest, tmp, size);  // 将临时空间tmp的内容拷贝到dest
        free(tmp);  // 释放内存
        return dest;
    }
    else
        return NULL;
}

void input(Book books[], char *line)
{
    int i;
    char *p = line;
    if(*p)
    {
        for(i=0;*(p+i)!=' ';i++);
        myStrncpy(books[num].na, p, i);
        p += i+1;
        for(i=0;*(p+i)!=' ';i++);
        myStrncpy(books[num].au, p, i);
        p += i+1;
        for(i=0;*(p+i)!=' ';i++);
        myStrncpy(books[num].cn, p, i);
        p += i+1;
        for(i=0;*(p+i)!='\0' && *(p+i)!='\n';i++);
        myStrncpy(books[num].dat, p, i);
    }
    num++;
}